public class Solution {
    class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
            next = null;
        }
    }

    //判断环形链表
    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) {
            return false;
        }
        ListNode slow = head;
        ListNode first = head.next;
        while (slow != first) {
            if (first == null || first.next == null) {
                return false;
            }
            slow = slow.next;
            first = first.next.next;
        }
        return true;
    }
}
